
TOOL.Category		= "Luftkreig"
TOOL.Name			= "#Multi Weld"
TOOL.Command		= nil
TOOL.ConfigName		= ""
TOOL.Selection		= {}
TOOL.ColorMemory	= {}

if ( CLIENT ) then
	language.Add( "tool.lk_link.name", "Multi Weld" )
	language.Add( "tool.lk_link.desc", "Weld multiple entities to a single entity" )
	language.Add( "tool.lk_link.0", "Left click to select entites, right click to weld. Reload to clear the tool's selection." )
end

-- since color is entity-wide, don't set the color per bone
function TOOL:DeselectColor(ent)
	if (!ent || !ent.IsValid || !ent:IsValid()) then return false end

	-- if entity is in color memory, decrement the counter until zero, then reset the color
	for it, item in ipairs(self.ColorMemory) do
		if (item.ent == ent) then
			item.instances = item.instances - 1
			
			if (item.instances <= 0) then
				item.instances = 0
				ent:SetColor(item.r, item.g, item.b, item.a)
				table.remove(self.ColorMemory, it)
			end 
			
			return true
		end
	end
	
	return false
end

function TOOL:SelectColor(ent)
	if (!ent || !ent.IsValid || !ent:IsValid()) then return false end

	-- if entity is already in color memory, increment the counter 
	for _, item in pairs(self.ColorMemory) do
		if (item.ent == ent) then
			item.instances = instances + 1
			return true
		end
	end
	
	-- otherwise, add to color memory
	local item = {}
	item.ent = ent
	item.r, item.g, item.b, item.a = ent:GetColor()
	ent:SetColor(70, 255, 70, 128)
	item.instances = 1
	table.insert(self.ColorMemory, item)
	
	return true
end

function TOOL:SelectEntity(ent, bone)
	if (!ent || !ent.IsValid || !ent:IsValid()) then return false end

	local item = {}
	item.ent = ent
	item.bone = bone
	table.insert(self.Selection, item)
	self:SelectColor(ent)
	return true
end

function TOOL:DeselectEntity(ent, bone)
	if (!ent || !ent.IsValid || !ent:IsValid()) then return false end

	for it, item in ipairs(self.Selection) do
		if (item.ent == ent && item.bone == bone) then
			self:DeselectColor(ent)
			table.remove(self.Selection, it)
			return true	
		end
	end 
	
	return false
end

function TOOL:IsSelected(ent, bone)
	for _, item in pairs(self.Selection) do
		if (item.ent == ent && item.bone == bone) then
			return true	
		end
	end
	
	return false
end

function TOOL:ClearSelection()
	for _, item in pairs(self.ColorMemory) do
		item.ent:SetColor(item.r, item.g, item.b, item.a)
	end
	
	self.ColorMemory = {}
	self.Selection = {}
end

function TOOL:LeftClick( trace )
	
	if (!trace.Entity ) then return false end
	if (!trace.Entity:IsValid()) then return false end
		
	if (SERVER && !util.IsValidPhysicsObject(trace.Entity, trace.PhysicsBone)) then return false end

	local ent = trace.Entity
	local bone = trace.PhysicsBone

	if (self:IsSelected(ent, bone)) then
		return self:DeselectEntity(ent, bone)
	else
		return self:SelectEntity(ent, bone)
	end

	return true
	
end

function TOOL:RightClick( trace )
	if (CLIENT) then return true end
	
	local core = trace.Entity

	if (table.getn(self.Selection) > 0) then
		local constraints = {}
		
		local count = table.getn(self.Selection)
		
		for i = 1, count, 1 do
			local item1 = self.Selection[i]
				local constraint = constraint.Weld(item1.ent, core, item1.bone, trace.PhysicsBone)
				table.insert(constraints, constraint)		
		end

		self:ClearSelection()
		return true
		
	elseif (table.getn(self.ColorMemory) > 0) then
		-- just in case
		self:ClearSelection()
		return false
	else
		return false
	end

end

function TOOL:Reload( trace )
	if (CLIENT) then return true end
	
		if (table.getn(self.Selection) > 0 || table.getn(self.ColorMemory) > 0) then
			self:ClearSelection()
			return true
		else
			return false
		end
	end



function TOOL.BuildCPanel( panel )	
end
